package com.my.blog.website.service.impl;

import com.my.blog.website.dao.QuestionCommentsVoMapper;
import com.my.blog.website.modal.Vo.QuestionCommentsVo;
import com.my.blog.website.modal.Vo.QuestionCommentsVoExample;
import com.my.blog.website.service.IQuestionCommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：gyh
 * @date ：Created in 2020/9/22 23:38
 * @description：问答评论服务实现类
 * @modified By：gyh
 * @version: 1.0
 */
@Service
public class QuestionCommentServiceImpl implements IQuestionCommentService {


    @Resource
    QuestionCommentsVoMapper commentsVoMapper;

    //存放迭代出的所有子评论
    private List<QuestionCommentsVo> replys = new ArrayList<>();


    /**
     * @Author: gyh
     * @Description: 查询评论列表
     * @DateTime: 2020/9/22 23:39
     * @Params: []
     * @Return java.util.List<com.my.blog.website.modal.Vo.QuestionCommentsVo>
     */
    @Override
    public List<QuestionCommentsVo> listComment(String qid) {
        //查找父评论 parentCommentId为0父节点
        QuestionCommentsVoExample example = new QuestionCommentsVoExample();
        QuestionCommentsVoExample.Criteria criteria = example.createCriteria();
        criteria.andParentCommentIdEqualTo("0");
        criteria.andQuestionIdEqualTo(qid);
        example.setOrderByClause("create_time desc");
        List<QuestionCommentsVo> questionCommentsVoList = commentsVoMapper.selectByExampleWithBLOBs(example);
        for (QuestionCommentsVo commentsVo : questionCommentsVoList) {
            String id = commentsVo.getId();
            String parentUserName = commentsVo.getUserName();

            QuestionCommentsVoExample childExample = new QuestionCommentsVoExample();
            QuestionCommentsVoExample.Criteria childCriteria = childExample.createCriteria();
            childCriteria.andParentCommentIdEqualTo(id);
            childCriteria.andParentCommentIdNotEqualTo("0");
            childCriteria.andQuestionIdEqualTo(qid);
            childExample.setOrderByClause("create_time desc");
            List<QuestionCommentsVo> childComments = commentsVoMapper.selectByExampleWithBLOBs(childExample);
            //查询子评论
            queryChildren(childComments, parentUserName, qid);
            commentsVo.setReplyComments(replys);
            replys = new ArrayList<>();
        }
        return questionCommentsVoList;
    }

    /**
     * @Author: gyh
     * @Description: 查询子评论
     * @DateTime: 2020/9/23 0:05
     * @Params: [childComments, parentUserName]
     * @Return void
     */
    private void queryChildren(List<QuestionCommentsVo> childComments, String parentUserName, String qid) {
        //判断是否有一级子回复
        if (childComments.size() > 0) {
            //循环找出子评论的id
            for (QuestionCommentsVo childComment : childComments) {
                String userName = childComment.getUserName();
                childComment.setParentUserName(parentUserName);
                replys.add(childComment);
                String childCommentId = childComment.getId();
                //查询二级以下所有子回复
                recursivelyChild(childCommentId, userName, qid);
            }
        }
    }

    /**
     * @Author: gyh
     * @Description: 循环迭代找出子回复
     * @DateTime: 2020/9/23 0:17
     * @Params: [childCommentId, userName]
     * @Return void
     */
    private void recursivelyChild(String childCommentId, String userName, String qid) {
        //根据子一级评论的id找到二级评论
        QuestionCommentsVoExample example = new QuestionCommentsVoExample();
        QuestionCommentsVoExample.Criteria criteria = example.createCriteria();
        criteria.andParentCommentIdEqualTo(childCommentId);
        criteria.andQuestionIdEqualTo(qid);
        example.setOrderByClause("create_time desc");
        List<QuestionCommentsVo> replayComments = commentsVoMapper.selectByExampleWithBLOBs(example);

        if (replayComments.size() > 0) {
            for (QuestionCommentsVo replayComment : replayComments) {
                String parentUserName = replayComment.getUserName();
                replayComment.setParentUserName(userName);
                String replayId = replayComment.getId();
                replys.add(replayComment);
                //循环迭代找出子集回复
                recursivelyChild(replayId, parentUserName, qid);
            }
        }
    }

    /**
     * @Author: gyh
     * @Description: 保存评论
     * @DateTime: 2020/9/22 23:39
     * @Params: [comment]
     * @Return int
     */
    @Override
    public int saveComment(QuestionCommentsVo comment) {
        comment.setCreateTime(new Date());
        return commentsVoMapper.insert(comment);
    }
}
